import $service from '../../../../service.js';
var hospital_id = "1";
var that;
var doctor_id;
var detailsList = []
var diagnosis_id;
var prescription_price;
var guidance_content;//指导内容
Page({
    data: {
        icd_id: '',
        icd_name: '',
        bz: '',
        prescription_price: '0',
        prescription_name: '',
        prescription_taking: '', //服用方法
        prescription_requirements: '', //服用要求
        prescription_num: '', //付数
        list: [],
        list2: [],
        diagnosisList: [],
        queryName: '',
        isullist: false,
        drug_type: 0 ,//1中药 2西药
        type:0
    },
    onLoad(e) { //
        that = this.data;
        if(!$service.isUndefinedAndEmpty(e.type)){
            if(e.type == 1){
                guidance_content = e.guidance_content
            }
            this.setData({
                type: e.type
            })
        }else{
            detailsList = $service.strToJson(e.detailsList);
            prescription_price = e.prescription_price;
            this.setData({
                drug_type: e.drug_type
            })
        }
        
        
    },
    onShow() {
        that = this.data;
        doctor_id = $service.getUserLocalStorage().doctor_id;
        if (!$service.isUndefinedAndEmpty($service.getUserLocalStorage().hospital_id)) {
            hospital_id = $service.getUserLocalStorage().hospital_id
        }
        this.publicUtil_getUtilTable() //药品详情
    },
    
    //list
    publicUtil_getUtilTable: function() {
        var jsonData = {
            "type": "7,8", //类型 // type 类型  1血糖测量条件 2西药药品用法 3药品使用频率 4系统医院 5科室表  6中药特殊服用用法 7中药服用方法 8中药服用要求
            "hospital_id": hospital_id, //医院id
        }
        var jsonAjax = {
            "url": "publicUtil_getUtilTable.do",
            "jsonData": jsonData,
        }
        $service.getAjaxData(jsonAjax, this.publicUtil_getUtilTable_back);
    },
    //回调
    publicUtil_getUtilTable_back: function(jsonBack) {
        var data = $service.strToJson(jsonBack.data).data;
        var list = data.list;
        if (!$service.isUndefinedAndEmpty(data.list2)) {
            that.list2 = data.list2
        }
        this.setData({
            list: list,
            list2: that.list2
        })
    },
    tap_usage(e) {
        var num = e.currentTarget.dataset.index;
        if (num == 0) { //服用方法
            that.islist = !that.islist
        } else if (num == 1) { //服用要求
            that.islist2 = !that.islist2
        }
        this.setData({
            islist: that.islist,
            islist2: that.islist2
        })
    },
    //选择用法、用量单位、频次、嘱托
    chooselist(e) {
        var index = e.currentTarget.dataset.index;
        var num = e.currentTarget.dataset.num;
        if (num == 0) { //用法
            that.prescription_taking = that.list[index].usage_name
        } else if (num == 1) { //用量单位
            that.prescription_requirements = that.list2[index].usage_name
        }
        this.setData({
            prescription_taking: that.prescription_taking,
            prescription_requirements: that.prescription_requirements,
            islist: false,
            islist2: false
        })
    },
    //input监听
    inputChange(e) {
        var index = e.currentTarget.dataset.index;
        if (index == 1) { //用量
            this.setData({
                queryName: e.detail.value
            })
            this.doctorSide_personal_prescription_getDiagnosisList()
        } else if (index == 2) { //天数
            that.prescription_name = e.detail.value
        } else if (index == 3) { //盒数
            that.prescription_num = e.detail.value
        } else if (index == 4) { //嘱托
            that.bz = e.detail.value
        }
        this.setData({
            prescription_name: that.prescription_name,
            prescription_num: that.prescription_num,
            bz: that.bz
        })
    },
    doctorSide_personal_prescription_getDiagnosisList: function() {
        var jsonData = {
            "hospital_id": hospital_id,
            "queryName": this.data.queryName
        }
        var jsonAjax = {
            "url": "doctorSide_personal_prescription_getDiagnosisList.do",
            "jsonData": jsonData,
        }
        $service.getAjaxData(jsonAjax, this.doctorSide_personal_prescription_getDiagnosisList_back);
    },
    doctorSide_personal_prescription_getDiagnosisList_back: function(jsonBack) {
        var data = $service.strToJson(jsonBack.data).data;
        var diagnosisList = data.diagnosisList; //列表
        if (diagnosisList.length == 0) {
            that.isullist = false;
            this.setData({
                icd_name: that.icd_name
            })
            $service.showToast('暂无数据，请输入正确的诊断名称')
        } else {
            that.isullist = true
        }
        this.setData({
            diagnosisList: diagnosisList,
            isullist: that.isullist
        })
    },
    tapdiagnosisList(e) {
        var index = e.currentTarget.dataset.index;
        diagnosis_id = that.diagnosisList[index].diagnosis_id
        if($service.isUndefinedAndEmpty(that.prescription_name)){
            that.prescription_name = that.diagnosisList[index].diagnosis_name
        }
        this.setData({
            icd_name: that.diagnosisList[index].diagnosis_name,
            prescription_name: that.prescription_name,
            icd_id: that.diagnosisList[index].icd_id,
            isullist: false
        })
    },
    cancel(e) {
        this.setData({
            icd_name: that.icd_name,
            isullist: false
        })
    },
    qrBtn() {
        if ($service.isUndefinedAndEmpty(that.icd_name)) {
            $service.showToast('请先选择诊断名称');
            return;
        }
        if ($service.isUndefinedAndEmpty(that.prescription_name)) {
            $service.showToast('请先输入模板名称');
            return;
        }
        if(that.type == 1){//健康指导
            var jsonData={
                "doctor_id" : doctor_id,
                "guidance_name":that.prescription_name,
                "hospital_id":hospital_id
            }
            var jsonAjax = {
                "url": "doctorSide_personal_inquiry_getHealthGuidanceTemplate.do",
                "jsonData": jsonData,
            }
            $service.getAjaxData(jsonAjax, this.getHealthGuidanceTemplate_back)
        }else{
            var jsonData = {
                "hospital_id": hospital_id, //医院id
                "icd_id": that.icd_id, //疾病分类id
                "diagnosis_id": diagnosis_id, //	诊断id
                "doctor_id": doctor_id, //医生主键 0为公告模板
                "prescription_type": that.drug_type, //处方类型 1中药 2西药
                "prescription_price": prescription_price, //	处方金额
                "prescription_name": that.prescription_name, //处方名称 中药时使用
                "bz": that.bz, //备注
                "detailsList": $service.jsonToStr(detailsList)
            }
            if (that.drug_type == 1) { //1中药
                if ($service.isUndefinedAndEmpty(that.prescription_taking)) {
                    $service.showToast('请先选择服用方法');
                    return;
                }
                if ($service.isUndefinedAndEmpty(that.prescription_num)) {
                    $service.showToast('请先输入付数');
                    return;
                }
                jsonData.prescription_num = that.prescription_num; //付数 中药时使用
                jsonData.prescription_taking = that.prescription_taking; //服用方法 中药时使用
                jsonData.prescription_requirements = that.prescription_requirements; //服用要求 中药时使用
            }
            var jsonAjax = {
                "url": "doctorSide_personal_prescription_addUtilPrescription.do",
                "jsonData": jsonData,
            }
            $service.getAjaxData(jsonAjax, function(jsonObj) {
                $service.showToast('保存模板成功')
                setTimeout(function() {
                    wx.navigateBack({
                        delta: 1
                    })
                }, 1500);
            });
        }
    },
    getHealthGuidanceTemplate_back(jsonBack){
         var data = $service.strToJson(jsonBack.data).data;
    	var healthGuidanceTemplateList = data.healthGuidanceTemplateList;
    	var eachcount = 0;
        for (var i = 0; i< healthGuidanceTemplateList.length; i++) {
            if(healthGuidanceTemplateList[i].guidance_name==that.prescription_name){
                $service.showToast("模板名称重复!")
                return false;
            }else {
                eachcount++;
            }
        }
    	if($service.isUndefinedAndEmpty(that.prescription_name)){
    		$service.showToast("请输入模板名称!")
    		return;
    	}
    	if(eachcount>=healthGuidanceTemplateList.length){
    		var jsonData={
    			"icd_id" : that.icd_id,
    			"diagnosis_id" : diagnosis_id,
    			"doctor_id" : doctor_id,
    			"guidance_name" : that.prescription_name,
    			"guidance_content" : guidance_content
    		}
            var jsonAjax = {
                "url": "doctorSide_personal_inquiry_addHealthGuidanceTemplate.do",
                "jsonData": jsonData,
            }
            $service.getAjaxData(jsonAjax, function(jsonObj) {
                $service.showToast('保存模板成功')
                setTimeout(function() {
                    wx.navigateBack({
                        delta: 1
                    })
                }, 1000);
            });
    	}
    }
})
